# !/usr/bin/

import subprocess


cmd = ['pip3', 'list', '-o', '-i', 'https://pypi.douban.com/simple']
update_list = []
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT, encoding="utf-8")
while proc.poll() is None:  # 检查子进程是否被终止
    out = proc.stdout.readline().strip()
    name = out.split()
    try:
        if len(name) != 0:
            if name[0] == "Package":
                print("Package str is not a package name")
                continue
            elif name[0].find('--') != -1:
                print("---- str is not a package name")
                continue
            update_list.append(name[0])
    except Exception as e:
        print(name)

print(update_list)

update_cmd = ['pip3', 'install', '-U', '', '-i', 'https://pypi.douban.com/simple']

for package in update_list:
    update_cmd[3] = package
    proc = subprocess.Popen(update_cmd, stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT, encoding="utf-8")
    while proc.poll() is None:  # 检查子进程是否被终止
        out = proc.stdout.readline().strip()
        print(out)
    print("{} is install".format(package))


